angular.module('starter')

.factory('VoteService', function(ResourceVisitService,$q,$ionicPopup) {
  // Might use a resource here that returns a JSON array
        var voteService =  {
            voteObj : {
                voteTypeList : [],
                voteDefines :{}
            },
            userModel:{
                userName:''
            },
            findVoteType:function(voteTypeId){
                var voteType = {};
                angular.forEach(voteService.voteObj.voteTypeList,function (voteTypeDefine){
                    if(voteTypeId == voteTypeDefine.voteType){
                        voteType = voteTypeDefine;
                    }
                });
                return voteType;
            },
            setUserName:function(userName){
                voteService.userModel.userName=userName;
            },
            getUserName:function(){
                return voteService.userModel.userName;
            },
            init:function (voteDefineType,voteDefineList){
                if(voteService.voteObj.voteDefines[voteDefineType]&& voteService.voteObj.voteDefines[voteDefineType].length>0){
                    angular.forEach(voteService.voteObj.voteDefines[voteDefineType],function (data){
                        voteDefineList.push(data);
                    });
                    return ;
                }
                voteService.voteObj.voteDefines[voteDefineType]=voteDefineList;

                var voteResource = ResourceVisitService.getResourceService('db/localH2/voteDefine')
                voteResource.queryList({voteDefineType:voteDefineType},{sort:'sort',order:'asc'}).then(
                    function (result){
                        console.log(result);
                        angular.forEach(result,function (data){
                            var voteItem = {
                                voteDefineId:data.voteDefineId,
                                voteName:data.voteDefineName,
                                vote:0,
                                choice:false,
                                srcChoice:false,
                                initChoice:false,
                                badgeCss : 'badge badge-royal',
                                voteDefineType:data.voteDefineType
                            }
                            voteService.voteObj.voteDefines[voteDefineType].push(voteItem);

                        });
                    },function (){
                        console.log("error");
                    }
                ).then(function (){
                        var voteStatisticsResource = ResourceVisitService.getResourceService('db/localH2/voteStatisticsByType');
                        voteStatisticsResource.queryList({voteDefineType:voteDefineType}).then(
                            function (result){
                                angular.forEach(result,function (data){
                                    angular.forEach(voteService.voteObj.voteDefines[voteDefineType],function (city){
                                        if(data.voteDefineId ==city.voteDefineId){
                                            city.vote = data.voteNum;

                                        }
                                    });
                                });
                            },function (){
                                console.log("error");
                            }
                        ).then(function (){
                                var voteQueryResource = ResourceVisitService.getResourceService('db/localH2/vote');
                                voteQueryResource.queryList({voteDefineType:voteDefineType,voter:voteService.userModel.userName}).then(
                                    function (result){
                                        angular.forEach(result,function (data){
                                            angular.forEach(voteService.voteObj.voteDefines[voteDefineType],function (city){
                                                if(data.voteDefineId ==city.voteDefineId){
                                                    city.initChoice= true;
                                                    city.choice= true;
                                                    city.srcChoice= true;
                                                    city.voteId=data.voteId;
                                                }
                                            });
                                        });
                                    }
                                ) .then(function (){
                                        console.log('ok');
                                    });
                         });

                 });

            },


            choice: function(voteDefineType){
                console.log('choice');
                angular.forEach(voteService.voteObj.voteDefines[voteDefineType],function (data){
                    if(data.choice != data.srcChoice){
                        if(data.choice ){
                            data.vote  ++;
                        }else{
                            data.vote  --;
                        }
                        data.srcChoice= data.choice;
                    }
                });
            },
            submitAll:function (){
                var voteResource = ResourceVisitService.getResourceService('db/localH2/vote')

                angular.forEach(voteService.voteObj.voteDefines,function (voteDefineArray){
                    angular.forEach(voteDefineArray,function (vote){

                        if(vote.choice != vote.initChoice){
                            if(vote.choice){
                                var data= {
                                    voter:voteService.userModel.userName,
                                    voteDefineId:vote.voteDefineId,
                                    voteNum:'1',
                                    voteDefineType:vote.voteDefineType
                                };
                                voteResource.saveResource(data);
                            }else{
                                voteResource.deleteResource(vote.voteId);
                            }
                            vote.initChoice = vote.choice;
                            vote.srcChoice = vote.choice;
                        }
                    });
                    voteService.alertPopup('提交完成');
                });
            },
            findVoteDefineType:function(){
                var voteQueryListResource = ResourceVisitService.getResourceService('db/localH2/voteType');

               return  voteQueryListResource.queryList({},{sort:'sort',order:'asc'}).then(
                   function (result){
                       voteService.voteObj.voteTypeList=result;
                       return result;
                   },function (){

                   }
               );
            },
            alertPopup :function (message){
                $ionicPopup.alert({
                    title: '提示',
                    template: message,
                    okText:'关闭'
                });
            }

        };
        return voteService;
    });
